package com.tiantianyuegou.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.tiantianyuegou.R;
import com.tiantianyuegou.http.ApiDefine;


/**
 * Activity基类
 * Created by huyang on 2016/1/5 0005.
 */
public abstract class BaseActivity extends AppCompatActivity {

    public TextView mToolbarTitle;
    public Toolbar mToolbar;
    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;


    }

    /**
     * 初始化视图
     */
    public void initViews() {
        findViewByIds();
        initListener();
        initData();

    }


    /**
     * 寻找控件
     */
    public abstract void findViewByIds();

    /**
     * 控件事件监听
     */
    public abstract void initListener();

    /**
     * 初始化数据
     */
    public abstract void initData();

    public void setTitle(CharSequence title) {
        if (mToolbarTitle != null) {
            mToolbarTitle.setText(title);
        }
    }

    public void setTitle(int titleId) {
        setTitle(getString(titleId));
    }

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
        if (mToolbar != null) {
            setSupportActionBar(mToolbar);
            getSupportActionBar().setTitle("");
            getSupportActionBar().setSubtitle("");
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            mToolbar.setNavigationIcon(R.mipmap.title);
        }
    }

    /**
     * toast
     *
     * @param message
     */
    public void showMsg(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    public void showMsg(int message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public boolean onSupportNavigateUp() {
        finish();
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void loadData(HttpRequest.HttpMethod method, String url, RequestParams params,
                         RequestCallBack<String> callBack) {
        HttpUtils httpUtils = new HttpUtils();
        // 30秒超时
        httpUtils.configCurrentHttpCacheExpiry(1000 * 30);
        if (params == null) {
            params = new RequestParams();
        } else {
            params.addBodyParameter("key", ApiDefine.key);
            params.addBodyParameter("code", ApiDefine.code);
        }
        httpUtils.send(method, url, params, callBack);
    }

    // 隐藏软键盘
    protected void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

}
